﻿using ConfExpert.Forms.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class ToDo : XForm
    {
        public ToDo()
        {
            InitializeComponent();
        }

        private void ToDo2_Load(object sender, EventArgs e)
        {
            BindData(true);
            gvMaster.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gvDetail.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;



            GridManager gm = GridManager.GetManager(gvMaster);
            gm.GridMenu.AddMenu("İptal", SetStuts);
            if (ytk(170))
            {
                gm.GridMenu.AddMenu("Sıraya alındı", SetStuts);
                gm.GridMenu.AddMenu("Üzerinde çalışılıyor...", SetStuts);
                gm.GridMenu.AddMenu("Tamamlandı", SetStuts);
            }

            this.Text = "Kumaş Satınalma Modül Planlama";
            gvMaster.SetViewCaption("Yapılacaklar");
            gvDetail.SetViewCaption("Detaylar / Notlar");

            gvMaster.RowCellStyle += gvMaster_RowCellStyle;
            gvDetail.RowCellStyle += gvDetail_RowCellStyle;
            btnYenile.ItemClick += btnYenile_ItemClick;
            btnKaydet.ItemClick += btnKaydet_ItemClick;
        }

        void gvDetail_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            //  throw new NotImplementedException();
        }

        void gvMaster_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            string status = gvMaster.GetRowCellValue(e.RowHandle, "Status").ToStr();
            switch (status)
            {
                case "Tamamlandı":
                    if (e.Column.FieldName == "Status")
                        e.Appearance.BackColor = Color.LightBlue;
                    else
                        e.Appearance.ForeColor = Color.Gray;
                    break;
                case "Üzerinde çalışılıyor...":
                    if (e.Column.FieldName == "Status")
                        e.Appearance.BackColor = Color.LightSalmon;
                    else
                        e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                    break;
            }
        }




        private void SetStuts(object sender, EventArgs e)
        {
            string status = ((DevExpress.Utils.Menu.DXMenuItem)(sender)).Caption;
            gvMaster.SetFocusedRowCellValue("Status", status);
            DataTable dtDetail = gvDetail.DataTable();
            string ptaskId = gvMaster.GetFocusedRowCellValue("PTaskId").ToStr();
            DataRow[] drs = dtDetail.Select("PTaskId=" + ptaskId, "InsertedOn desc");
            string lastOne = "";
            string newOne = "Durum " + status + " olarak değiştirildi.";
            if (drs.Length > 0)
                lastOne = drs[0]["Subject"].ToStr();

            if (lastOne != newOne)
            {
                DataRow drNew = dtDetail.NewRow();
                drNew["Subject"] = newOne;
                drNew["CreatorId"] = WhoAmI;
                dtDetail.Rows.Add(drNew);
            }
        }

        private void BindData(bool firstTime)
        {
            DataSet ds = ExecuteDataSet("GetPTasks");

            DataTable dtMaster = ds.Tables[0];
            DataTable dtDetail = ds.Tables[1];

            dtMaster.PrimaryKey = new DataColumn[] { dtMaster.Columns["PTaskId"] };
            dtMaster.AddAutoIncrementalColumn("PTaskId");

            dtDetail.PrimaryKey = new DataColumn[] { dtDetail.Columns["PTaskDetailId"] };
            dtDetail.AddAutoIncrementalColumn("PTaskDetailId");

            if (firstTime)
            {
                gvMaster.Init(dtMaster);
                gvMaster.AddDetailGrid(gvDetail, "PTaskId");

                gvMaster.SetCaptions("PTaskId:#", "Subject:Konu", "CreatorId:Oluşturan", "InsertedOn:Eklenme Tarihi", "Status:Durum", "Priority:Öncelik", "ContactId:İlgili");
                gvMaster.Columns["CreatorId"].ColumnEdit = RI(Rp.V_User);
                gvMaster.Columns["CreatorId"].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

                gvDetail.Columns["CreatorId"].ColumnEdit = RI(Rp.V_User);
                gvMaster.Columns["ContactId"].ColumnEdit = RI(Rp.V_User);

                gvDetail.MakeColumnInvisible("PTaskId");
                gvDetail.SetCaptions("CreatorId:Oluşturan", "InsertedOn:Eklenme", "PTaskDetailId:#", "Subject:Detaylar / Notlar");
                gvMaster.FormatAsDate("InsertedOn");
            }
            else
            {
                int focusedRH = gvMaster.FocusedRowHandle;
                DevExpress.XtraGrid.Columns.GridColumn gcFocused = gvMaster.FocusedColumn;
                gvMaster.RefreshDataSource(dtMaster);
                gvDetail.RefreshDataSource(dtDetail);
                gvMaster.FocusedRowHandle = focusedRH;
                gvMaster.FocusedColumn = gcFocused;
            }

            gvMaster.MakeReadOnly("Subject");
            gvDetail.MakeReadOnly("Subject");

            if (ytk(169))
            {
                gvMaster.MakeEditable();
                gvDetail.MakeEditable();
            }


            dtMaster.TableNewRow += dtMaster_TableNewRow;
            dtDetail.TableNewRow += dtDetail_TableNewRow;

            gvMaster.BestFitColumns();
            gvDetail.BestFitColumns();
        }

        void dtDetail_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            int PTaskId = gvMaster.GetFocusedRowCellValue("PTaskId").ToInt(-1);
            //if (PTaskId < 0)
            //    BindData(false);

            //PTaskId = gvMaster.GetFocusedRowCellValue("PTaskId").ToInt(-1);
            e.Row["PTaskId"] = PTaskId;
            e.Row["CreatorId"] = WhoAmI;
            e.Row["InsertedOn"] = DateTime.Now;
        }

        void dtMaster_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["CreatorId"] = WhoAmI;
            e.Row["InsertedOn"] = DateTime.Now;
            e.Row["Status"] = "Yeni";

            //gvMaster.Update();
            //BindData(false);

        }

        private void gvMaster_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gvDetail.BestFitColumns();
        }

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvMaster.UpdateAll();

            gvMaster.BestFitColumns();
            gvDetail.BestFitColumns();
        }

        private void btnYenile_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            BindData(false);
        }
    }
}
